import openpyxl
from openpyxl import Workbook
from typing import List, Any

def write_xlsx_file(sheet_name: str, headers: List[str], data: List[List[Any]], file_name: str = 'example.xlsx') -> None:
    # 创建一个新的工作簿
    wb = Workbook()

    # 获取活动工作表
    ws = wb.active

    # 设置工作表名称
    ws.title = sheet_name

    # 写入表头
    ws.append(headers)

    # 写入数据
    for row in data:
        ws.append(row)

    # 保存工作簿
    wb.save(file_name)

def read_xlsx_file(file_name: str) -> List[dict]:
    # 打开现有的工作簿
    wb = openpyxl.load_workbook(file_name)

    # 获取活动工作表
    ws = wb.active

    # 获取表头
    headers = [cell.value for cell in ws[1]]

    # 读取数据
    data = []
    for row in ws.iter_rows(min_row=2):
        row_data = {}
        for header, cell in zip(headers, row):
            row_data[header] = cell.value
        data.append(row_data)

    return data



# 设置工作表名称、表头和数据
# sheet_name = 'test'
# headers = ['地址', '交易哈希']
# data = [
#     ['地址1', '哈希1'],
#     ['地址2', '哈希2'],
#     ['地址3', '哈希3'],
#     ['地址4', '哈希4'],
#     ['地址5', '哈希5']
# ]

# # 写入数据到xlsx文件中
# write_xlsx_file(sheet_name, headers, data, 'jj.xlsx')

# # 读取xlsx文件中的数据
# data = read_xlsx_file('jj.xlsx')

# # 输出读取到的数据
# print(data)
